package com.block.datapush.service.impl;

import com.alibaba.fastjson.JSON;
import com.block.common.service.IMerchantCommonService;
import com.block.common.service.LogFactory;
import com.block.datapush.factory.FilterFactory;
import com.block.datapush.model.param.FilterHandlerParam;
import com.block.datapush.model.result.FilterHandlerResult;
import com.block.datapush.service.IDataFilterHandleService;
import com.block.db.entity.MemberSourceInfo;
import com.block.db.entity.MerchantBaseInfo;
import com.block.db.entity.MerchantPushCountInfo;
import com.block.db.entity.MerchantTaskInfo;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class DataFilterHandleService implements IDataFilterHandleService {

    private static Logger pushLog = LogFactory.getPushLog();

    @Autowired
    IMerchantCommonService merchantCommonService;

    @Override
    public FilterHandlerResult execMerchantFilter(MerchantTaskInfo merchantTaskInfo, MemberSourceInfo memberSourceInfo){

//        MerchantBaseInfo merchantBaseInfo = merchantCommonService.getMerchantBaseInfo(merchantTaskInfo.getInfoId());

        Integer taskSettleNumber = 0;
        if (merchantTaskInfo.getPushNumber()<999){
            // 查询该任务已经推送了多少条了
            taskSettleNumber = merchantCommonService.getMerchantPushCountInfo(merchantTaskInfo.getId());
        }
        // 商户推送数量限制
//        Integer taskSettleNumber = merchantCommonService.getMerchantPushCountInfo(merchantTaskInfo.getId());

        FilterHandlerParam handlerParam = new FilterHandlerParam();
        handlerParam.setInfoId(merchantTaskInfo.getInfoId());

        handlerParam.setPushNumber(merchantTaskInfo.getPushNumber());
        handlerParam.setSuccessNumber(taskSettleNumber);

        handlerParam.setNotPushDayWeek(merchantTaskInfo.getNotPushDayWeek());

        handlerParam.setInChannelArray(merchantTaskInfo.getInChannelArray());
        handlerParam.setInChannelType(merchantTaskInfo.getInChannelType());
        handlerParam.setOutChannelArray(merchantTaskInfo.getOutChannelArray());

        handlerParam.setInCityArray(merchantTaskInfo.getCityInfo());
        handlerParam.setOutCityArray(merchantTaskInfo.getOutCityArray());

        handlerParam.setMinAge(merchantTaskInfo.getMinAge());
        handlerParam.setMaxAge(merchantTaskInfo.getMaxAge());
        handlerParam.setStartTime(merchantTaskInfo.getStarttime());
        handlerParam.setEndTime(merchantTaskInfo.getEndtime());

        handlerParam.setLoanAmount(merchantTaskInfo.getLoanAmount());
        handlerParam.setLoanTime(merchantTaskInfo.getLoanTime());
        handlerParam.setZhiMa(merchantTaskInfo.getZhima());
        handlerParam.setHuaBei(merchantTaskInfo.getHuabei());
        handlerParam.setBaiTiao(merchantTaskInfo.getBaitiao());
        handlerParam.setWeiLi(merchantTaskInfo.getWeili());

        handlerParam.setHouse(merchantTaskInfo.getHouse());
        handlerParam.setCar(merchantTaskInfo.getCar());
        handlerParam.setGjj(merchantTaskInfo.getGjj());
        handlerParam.setShebao(merchantTaskInfo.getShebao());
        handlerParam.setBaoxian(merchantTaskInfo.getBaoxian());
        handlerParam.setShouru(merchantTaskInfo.getShouru());
        handlerParam.setYongtu(merchantTaskInfo.getYongtu());
        handlerParam.setYuqi(merchantTaskInfo.getYuqi());
        handlerParam.setXinyongka(merchantTaskInfo.getXinyongka());
        handlerParam.setGongzuo(merchantTaskInfo.getGongzuo());
        handlerParam.setXueli(merchantTaskInfo.getXueli());

        handlerParam.setOrFilter(merchantTaskInfo.getOrFilter());

        FilterHandlerResult handlerResult = FilterFactory.execFilterChainService(handlerParam, memberSourceInfo);
        pushLog.info("【机构条件过滤】过滤完成【result】{}【uid】{}【taskId】{}【infoId】{}",
                JSON.toJSONString(handlerResult), memberSourceInfo.getUid(), merchantTaskInfo.getId(), merchantTaskInfo.getInfoId());

        return handlerResult;
    }

}
